// subpkg/activity/activity.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityList:[],
    date:'',
    date1:'',
    show: false,
    currentDate: new Date().getTime(),
    minDate: new Date(2023, 8, 1).getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },
  onConfirm(){
    this.setData({
      date:this.data.date1,
      show:false
    })
    this.queryActivity(this.data.date)
  },
  onCancel(){
    this.setData({
      show:false
    })
  },
  onInput(event) {
    var date = new Date(event.detail)
    var year = date.getFullYear()
    var month = date.getMonth()+1 <10 ? `0${date.getMonth()+1}` : date.getMonth()+1
    var day = date.getDate()<10?`0${date.getDate()}` : date.getDate()
    var currentDate = `${year}-${month}-${day}`
    console.log(currentDate)
    
    this.setData({
      date1: currentDate,
    });
  },
  show(){
    this.setData({
      show:true
    })
  },
  showPopup() {
    this.setData({ show: true });
  },

  
  //预览图片
  imgClick(e){
    var index = e.currentTarget.dataset.index
    console.log(index)
    var imgUrl = this.data.activityList[index].pictureUrl
    console.log(imgUrl)
    wx.previewImage({
      urls: imgUrl,
      success:(res=>{
        console.log(res)
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth()+1 <10 ? `0${date.getMonth()+1}` : date.getMonth()+1
    var day = date.getDate()<10?`0${date.getDate()}` : date.getDate()
    var currentDate = `${year}-${month}-${day}`

    this.setData({
      date:currentDate

    })
    this.queryActivity(currentDate)
  },
  queryActivity(currentDate){
    wx.request({
      url: 'https://zhixiao.site:8080/activity/info',
      data:{
        elderlyId:wx.getStorageSync('elderId'),
        date:currentDate
      },
      header:{
        'Cookie':wx.getStorageSync('JSESSIONID')
      },
      success:(res)=>{
        if(res.data.data===null){
          Toast(`无记录`)
          this.setData({
            activityList:[]
          })
         
        }
        else{
          this.setData({
            activityList:res.data.data
          })
        }
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})